package com.egdd.video.controller;

import com.egdd.video.common.api.CommonResult;
import com.egdd.video.mbg.model.User;
import com.egdd.video.service.iml.CategoryServiceImpl;
import com.egdd.video.service.iml.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Api(tags = "UserController",description = "用户操作相关")
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryController.class);

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ApiOperation("添加用户信息")
    @ResponseBody
    public CommonResult<User> addUser(@RequestBody User user){
        int count = userService.addUser(user);
        CommonResult<User> commonResult;
        if (count==1){
            return CommonResult.success(user);
        }else {
            return CommonResult.failed("用户添加失败");
        }
    }

    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/delUser",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<User> delUserById(@ApiParam(name = "id",value = "用户id",required = true) int id){
        int count = userService.delUserById(id);
        if (count==1){
            return CommonResult.success(null,"删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }
    }


    @ApiOperation(value = "更新用户信息需要追加用户id")
    @ResponseBody
    @RequestMapping(value = "/updateUserInfo/{userid}",method = RequestMethod.POST)
    public CommonResult<User> updateUserInfo(@ApiParam(name = "userid",value = "用户id",required = true) @PathVariable Integer userid, @RequestBody User user){
        int count = userService.updateUserInfo(userid,user);
        if (count==1){
            return CommonResult.success(null,"用户信息更新成功");
        }else {
            return CommonResult.failed("用户信息更新失败");
        }
    }

    @ApiOperation(value = "更新用户信息")
    @ResponseBody
    @RequestMapping(value = "/updateUserInfo",method = RequestMethod.POST)
    public CommonResult<User> updateUserInfo(@RequestBody User user){
        int count = userService.replaceUserInfo(user);
        if (count==1){
            return CommonResult.success(null,"用户信息更新成功");
        }else {
            return CommonResult.failed("用户信息更新失败");
        }
    }
}
